package com.c.ctgproject.ui.vehicleinspection

import android.app.ProgressDialog
import android.os.Bundle
import android.text.TextUtils
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.mode.StorageVehicles
import com.c.ctgproject.retrofit.Response
import com.c.ctgproject.ui.files.MainViewModel
import com.c.ctgproject.ui.storage.StorageHomeActivity
import com.c.ctgproject.utils.*
import com.jaeger.library.StatusBarUtil

import kotlinx.android.synthetic.main.activity_vehicleinspectiont.*
import kotlinx.android.synthetic.main.title.*
import java.util.HashMap

/**
 *
 *仓储初检 页面
 */

class VehicleInspectiontActivity : AppCompatActivity(), View.OnClickListener {

    private val statusColor: StatusColor = StatusColor()
    private var mainViewModel: MainViewModel? = null
    private var progressDialog: ProgressDialog? = null
    private var dialogUtils: DialogUtils? = null
    private var carPlate: String? = null //车牌号
    private var token: String? = null
    private var carId: String? = null
    private var battery = "0"//电瓶 1 - 有 0 - 无
    private var conveyorShaft = "0"//传送轴 1 - 有 0 - 无
    private var waterTank: String = "0"//水箱 1 - 有 0 - 无
    private var cab: String = "0"//驾驶室 1 - 有 0 - 无
    private var highPressurePump = "0"//高压泵 1 - 有 0 - 无
    private var bigBox = "0"//大箱 1 - 有 0 - 无
    private var engine = "0"//发动机 1 - 有 0 - 无
    private var crane = "0"//启动机 1 - 有 0 - 无
    private var transmissionCase = "0"//变速箱 1 - 有 0 - 无
    private var line = "0"//线路 1 - 有 0 - 无
    private var condenser = "0"//冷凝器 1 - 有 0 - 无
    private var carKey = "0"//钥匙 1 - 有 0 - 无
    private var compressor = "0"//压缩机 1 - 有 0 - 无
    private var steeringMachine = "0"//方向机 1 - 有 0 - 无
    private var alternator = "0"//发电机 1 - 有 0 - 无
    private var frontAxle = "0"//前桥 1 - 有 0 - 无
    private var rearAxle = "0" //后桥 1 - 有 0 - 无
    private var drivingLicense = "0"//行驶证 1 - 有 0 - 无
    private var registration = "0"//登记证 1 - 有 0 - 无
    private var idCardCopy = "0"//身份证/营业执照复印件 1 - 有 0 - 无
    private var ternaryCatalysis = "0"//三元催化 1 - 有 0 - 无

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_vehicleinspectiont)
        StatusBarUtil.setTranslucent(this)
        statusColor.setAndroidNativeLightStatusBar(this, false)
        setSupportActionBar(toolbar)
        supportActionBar?.setHomeButtonEnabled(true)
        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowTitleEnabled(false)
        carPlate = intent.getStringExtra("carPlate")
        token = Utils.getShared2(this, "token")
        event()
    }

    private fun event() {
        progressDialog = ProgressDialog(
            this@VehicleInspectiontActivity,
            R.style.AppTheme_Dark_Dialog
        )
        progressDialog!!.isIndeterminate = true
        progressDialog!!.setCanceledOnTouchOutside(false)
        progressDialog!!.setCancelable(true)
        confirm_btn.setOnClickListener(this)


        CK_1.setOnCheckedChangeListener { buttonView, isChecked ->
            battery = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_2.setOnCheckedChangeListener { buttonView, isChecked ->
            conveyorShaft = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_3.setOnCheckedChangeListener { buttonView, isChecked ->
            engine = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_4.setOnCheckedChangeListener { buttonView, isChecked ->
            cab = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_5.setOnCheckedChangeListener { buttonView, isChecked ->
            waterTank = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_6.setOnCheckedChangeListener { buttonView, isChecked ->
            frontAxle = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_7.setOnCheckedChangeListener { buttonView, isChecked ->
            rearAxle = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_8.setOnCheckedChangeListener { buttonView, isChecked ->
            carKey = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_9.setOnCheckedChangeListener { buttonView, isChecked ->
            highPressurePump = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_10.setOnCheckedChangeListener { buttonView, isChecked ->
            bigBox = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_11.setOnCheckedChangeListener { buttonView, isChecked ->
            crane = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_12.setOnCheckedChangeListener { buttonView, isChecked ->
            steeringMachine = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_13.setOnCheckedChangeListener { buttonView, isChecked ->
            line = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_14.setOnCheckedChangeListener { buttonView, isChecked ->
            transmissionCase = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_15.setOnCheckedChangeListener { buttonView, isChecked ->
            condenser = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        CK_16.setOnCheckedChangeListener { buttonView, isChecked ->
            alternator = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_17.setOnCheckedChangeListener { buttonView, isChecked ->
            compressor = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_18.setOnCheckedChangeListener { buttonView, isChecked ->
            drivingLicense = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_19.setOnCheckedChangeListener { buttonView, isChecked ->
            registration = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }

        CK_20.setOnCheckedChangeListener { buttonView, isChecked ->
            idCardCopy = if (isChecked) {
                "1"
            } else {
                "0"
            }
        }
        sychCB.setOnCheckedChangeListener { _, isChecked ->
            ternaryCatalysis = if (isChecked) "1" else "0"
        }

        mainViewModel = ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        ).get(MainViewModel::class.java)
        dialogUtils = DialogUtils()
        mainViewModel!!.primaryInspection(token!!, carPlate!!)
        mainViewModel!!.getStorageVehiclesdata().observe(this, Observer { response: Response<StorageVehicles> ->
            if (response.status != 0) {
                Toast.makeText(this, response.msg, Toast.LENGTH_LONG).show()
                finish()
            } else {
                toolbar_title.text = response.data.ownerPlate
                ownerPlate.text = response.data.ownerPlate
                carOwner.text = response.data.carOwner
                carBrand.setText(response.data.carBrand)
                carVin.setText(response.data.carVin)
                carColor.setText(response.data.carColor)
                CK_1.isChecked = response.data.battery == "1"
                CK_2.isChecked = response.data.conveyorShaft == "1"
                CK_3.isChecked = response.data.engine == "1"
                CK_4.isChecked = response.data.cab == "1"
                CK_5.isChecked = response.data.waterTank == "1"
                CK_6.isChecked = response.data.frontAxle == "1"
                CK_7.isChecked = response.data.rearAxle == "1"
                CK_8.isChecked = response.data.carKey == "1"
                CK_9.isChecked = response.data.highPressurePump == "1"
                CK_10.isChecked = response.data.bigBox == "1"
                CK_11.isChecked = response.data.crane == "1"
                CK_12.isChecked = response.data.steeringMachine == "1"
                CK_13.isChecked = response.data.line == "1"
                CK_14.isChecked = response.data.transmissionCase == "1"
                CK_15.isChecked = response.data.condenser == "1"
                CK_16.isChecked = response.data.alternator == "1"
                CK_17.isChecked = response.data.compressor == "1"
                CK_18.isChecked = response.data.drivingLicense == "1"
                CK_19.isChecked = response.data.registration == "1"
                CK_20.isChecked = response.data.idCardCopy == "1"
                sychCB.isChecked = response.data.ternaryCatalysis == "1"
                steelRingNum.setText(response.data.steelRingNum.handlerNull())
                aluminumRingNum.setText(response.data.aluminumRingNum.toString())
                tyreNum.setText(response.data.tyreNum.toString())
                plateNum.setText(response.data.plateNum.toString())
                carId = response.data.carId
            }
        })

        mainViewModel!!.getprimaryInspectiondata().observe(this, Observer { response: Response<*> ->
            if (response.status == 0) {
                progressDialog!!.dismiss()
                Toast.makeText(applicationContext, response.msg, Toast.LENGTH_SHORT).show()
                //成功后返回首页
                startActivity<StorageHomeActivity>()
            } else {
                progressDialog!!.dismiss()
                dialogUtils!!.showTwo(this@VehicleInspectiontActivity, "提示", response.msg.toString())
            }
        })
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    override fun onClick(v: View?) {
        if (v == confirm_btn) {
            //完成
            if (TextUtils.isEmpty(carBrand.text.toString())) {
                Toast.makeText(applicationContext, "车型不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(carVin.text.toString())) {
                Toast.makeText(applicationContext, "车架号不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(carColor.text.toString())) {
                Toast.makeText(applicationContext, "颜色不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(steelRingNum.text.toString())) {
                Toast.makeText(applicationContext, "钢圈数量不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(tyreNum.text.toString())) {
                Toast.makeText(applicationContext, "轮胎数量不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(aluminumRingNum.text.toString())) {
                Toast.makeText(applicationContext, "铝圈数量不能为空！", Toast.LENGTH_SHORT).show()
            } else if (TextUtils.isEmpty(plateNum.text.toString())) {
                Toast.makeText(applicationContext, "车牌数量不能为空！", Toast.LENGTH_SHORT).show()
            } else if (!NetworkUtils.isConnected(applicationContext)) {
                Toast.makeText(applicationContext, "请检查您的网络是否连接", Toast.LENGTH_SHORT).show()
            } else {
                progressDialog!!.setMessage("正在提交...")
                progressDialog!!.show()
                val map: MutableMap<String, String> =
                    HashMap()
                if (carId == null) {
                    showToast("车辆异常,请稍后再试")
                    finish()
                }
                map["carId"] = carId!!
                map["carBrand"] = carBrand.text.toString()
                map["carColor"] = carColor.text.toString()
                map["carVin"] = carVin.text.toString()
                map["battery"] = battery
                map["conveyorShaft"] = conveyorShaft
                map["waterTank"] = waterTank
                map["cab"] = cab
                map["highPressurePump"] = highPressurePump
                map["bigBox"] = bigBox
                map["engine"] = engine
                map["crane"] = crane
                map["transmissionCase"] = transmissionCase
                map["line"] = line
                map["condenser"] = condenser
                map["carKey"] = carKey
                map["compressor"] = compressor
                map["steeringMachine"] = steeringMachine
                map["alternator"] = alternator
                map["frontAxle"] = frontAxle
                map["rearAxle"] = rearAxle
                map["drivingLicense"] = drivingLicense
                map["registration"] = registration
                map["ternaryCatalysis"] = ternaryCatalysis
                map["idCardCopy"] = idCardCopy
                map["steelRingNum"] = steelRingNum.text.toString()
                map["aluminumRingNum"] = aluminumRingNum.text.toString()
                map["tyreNum"] = tyreNum.text.toString()
                map["plateNum"] = plateNum.text.toString()
                map["remark"] = mRemarkET.text.toString()
                mainViewModel!!.primaryInspectionpost(token!!, map)
            }

        }
    }

}